﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Text.RegularExpressions;
using SubsidyApp.Models;

namespace SubsidyApp.Helpers
{
    public static class AppHelper
    {
        public static string ContentRoot
        {
            get
            {
                string ContentVirtualRoot = "~/Content";
                return VirtualPathUtility.ToAbsolute(ContentVirtualRoot);
            }
        }

        public static string ScriptsRoot
        {
            get
            {
                string ScriptsVirtualRoot = "~/Scripts";
                return VirtualPathUtility.ToAbsolute(ScriptsVirtualRoot);
            }
        }

        public static string ImgRoot
        {
            get { return string.Format("{0}/{1}", ContentRoot, "images"); }
        }

        public static string CssRoot
        {
            get { return string.Format("{0}/{1}", ContentRoot, "css"); }
        }

        public static string ImgUrl(string image)
        {
            string result = string.Format("{0}/{1}", ImgRoot, image);
            return result;
        }

        public static string CssUrl(string cssFile)
        {
            string result = string.Format("{0}/{1}", CssRoot, cssFile);
            return result;
        }

        public static string ScriptUrl(string scriptFile)
        {
            string result = string.Format("{0}/{1}", ScriptsRoot, scriptFile);
            return result;
        }

        public static string OrgName
        {
            get { return "Областной центр жилищных субсидий, социальных выплат и льгот"; }
        }

        public static string ShortOrgName
        {
            get { return "БУ \"ОЦЖССВиЛ\""; }
        }

        public static string ActionImage(this HtmlHelper html, string action, string controller, object routeValues, string imageRelativeUrl, string alt)
        {
            UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext);

            return string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"{2}\"/></a>",
                urlHelper.Action(action, controller, routeValues), imageRelativeUrl, alt); //urlHelper.Content();
        }

        // This function converts HTML code to plain text
        // Any step is commented to explain it better
        // You can change or remove unnecessary parts to suite your needs
        public static string HTMLToText(string HTMLCode)
        {
            // Remove new lines since they are not visible in HTML
            HTMLCode = HTMLCode.Replace("\n", " ");

            // Remove tab spaces
            HTMLCode = HTMLCode.Replace("\t", " ");

            // Remove multiple white spaces from HTML
            HTMLCode = Regex.Replace(HTMLCode, "\\s+", " ");

            // Remove HEAD tag
            HTMLCode = Regex.Replace(HTMLCode, "<head.*?</head>", ""
                                , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Remove any JavaScript
            HTMLCode = Regex.Replace(HTMLCode, "<script.*?</script>", ""
              , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Replace special characters like &, <, >, " etc.
            StringBuilder sbHTML = new StringBuilder(HTMLCode);
            // Note: There are many more special characters, these are just
            // most common. You can add new characters in this arrays if needed
            string[] OldWords = {"&nbsp;", "&amp;", "&quot;", "&lt;",
                "&gt;", "&reg;", "&copy;", "&bull;", "&trade;"};
            string[] NewWords = { " ", "&", "\"", "<", ">", "Â®", "Â©", "â€¢", "â„¢" };
            for (int i = 0; i < OldWords.Length; i++)
            {
                sbHTML.Replace(OldWords[i], NewWords[i]);
            }

            // Check if there are line breaks (<br>) or paragraph (<p>)
            sbHTML.Replace("<br>", "\n<br>");
            sbHTML.Replace("<br ", "\n<br ");
            sbHTML.Replace("<p ", "\n<p ");

            // Finally, remove all HTML tags and return plain text
            return System.Text.RegularExpressions.Regex.Replace(
              sbHTML.ToString(), "<[^>]*>", "");
        }

        /*
        public string RemoveHTMLTags(string HTMLCode)
        {
            return System.Text.RegularExpressions.Regex.Replace(
              HTMLCode, "<[^>]*>", "");
        }
         */
    }
}